package com.mdd.front.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.service.WxPayService;
import com.mdd.common.entity.system.SystemConfig;
import com.mdd.common.enums.ClientEnum;
import com.mdd.common.mapper.system.SystemConfigMapper;
import com.mdd.front.service.system.ISystemDevPayService;
import com.mdd.front.vo.system.SystemDevWxPayConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
public class WxPayServiceUtil {

    private static WxPayService wxPayService;

    private static WxPayService wxOaPayService;

    private static SystemConfigMapper systemConfigMapper;

    private static ISystemDevPayService systemDevPayService;


    @Resource
    public void setWxPayService(WxPayService wxPayService) {
        WxPayServiceUtil.wxPayService = wxPayService;
    }

    @Resource
    public void setWxOaPayService(WxPayService wxOaPayService) {
        WxPayServiceUtil.wxOaPayService = wxOaPayService;
    }

    @Resource
    public void setSystemConfigMapper(SystemConfigMapper systemConfigMapper) {
        WxPayServiceUtil.systemConfigMapper = systemConfigMapper;
    }

    @Resource
    public void setSystemDevPayService(ISystemDevPayService systemDevPayService) {
        WxPayServiceUtil.systemDevPayService = systemDevPayService;
    }

    /**
     * 对象句柄
     *
     * @author panweiliang
     * @return WxPayService
     */
    public static WxPayService handler(String terminal) {

        if (String.valueOf(ClientEnum.OA.getCode()).equalsIgnoreCase(terminal)) {
            resetWxOaPayConfig();
            return wxOaPayService;
        } else {
            resetWxPayConfig();
            return wxPayService;
        }

    }

    private static void resetWxPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        SystemDevWxPayConfig systemDevWxPayConfig = systemDevPayService.getWxPayConfig(1);
        payConfig.setAppId(StringUtils.trimToNull(getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(systemDevWxPayConfig.getMch_id()));
        payConfig.setApiV3Key(StringUtils.trimToNull(systemDevWxPayConfig.getPay_sign_key()));
        payConfig.setPrivateKeyContent(StringUtils.trimToNull(systemDevWxPayConfig.getApiclient_key()).getBytes());
        payConfig.setPrivateCertContent(StringUtils.trimToNull(systemDevWxPayConfig.getApiclient_cert()).getBytes());
        payConfig.setSignType(WxPayConstants.SignType.HMAC_SHA256);
//        payConfig.setMchKey(systemDevWxPayConfig.getPay_sign_key());
//        payConfig.set
        wxPayService.setConfig(payConfig);
    }

    private static void resetWxOaPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        SystemDevWxPayConfig systemDevWxPayConfig = systemDevPayService.getWxPayConfig(1);
        payConfig.setAppId(StringUtils.trimToNull(getOaAppId()));
        payConfig.setMchId(StringUtils.trimToNull(systemDevWxPayConfig.getMch_id()));
        payConfig.setApiV3Key(StringUtils.trimToNull(systemDevWxPayConfig.getPay_sign_key()));
        payConfig.setPrivateKeyContent(StringUtils.trimToNull(systemDevWxPayConfig.getApiclient_key()).getBytes());
        payConfig.setPrivateCertContent(StringUtils.trimToNull(systemDevWxPayConfig.getApiclient_cert()).getBytes());
        payConfig.setSignType(WxPayConstants.SignType.HMAC_SHA256);
        payConfig.setMchKey(systemDevWxPayConfig.getPay_sign_key());
        wxOaPayService.setConfig(payConfig);
    }

    private static String getAppId() {
        List<SystemConfig> configs = systemConfigMapper.selectList(
                new QueryWrapper<SystemConfig>()
                        .select("id", "type", "name", "value")
                        .eq("type", "mp_channel"));
        Map<String, String> map = new LinkedHashMap<>();
        for (SystemConfig config : configs) {
            map.put(config.getName(), config.getValue());
        }
        return map.getOrDefault("appId", "");
    }

    private static String getOaAppId() {
        List<SystemConfig> configs = systemConfigMapper.selectList(
                new QueryWrapper<SystemConfig>()
                        .select("id", "type", "name", "value")
                        .eq("type", "oa_channel"));
        Map<String, String> map = new LinkedHashMap<>();
        for (SystemConfig config : configs) {
            map.put(config.getName(), config.getValue());
        }
        return map.getOrDefault("appId", "");
    }

}
